package projcet.test.student.student;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class AddActivity extends AppCompatActivity {


    @BindView(R.id.addViewTop)
    TextView addViewTop;
    @BindView(R.id.backList)
    ImageButton backList;
    @BindView(R.id.addText1)
    EditText addText1;
    @BindView(R.id.line1)
    LinearLayout line1;
    @BindView(R.id.addText2)
    EditText addText2;
    @BindView(R.id.line2)
    LinearLayout line2;
    @BindView(R.id.addText3)
    EditText addText3;
    @BindView(R.id.line3)
    LinearLayout line3;
    @BindView(R.id.addText4)
    EditText addText4;
    @BindView(R.id.line5)
    LinearLayout line5;
    @BindView(R.id.spn_subjet)
    Spinner spnSubjet;
    @BindView(R.id.line4)
    LinearLayout line4;
    @BindView(R.id.submitBtn)
    Button submitBtn;
    private DbOpenHelper mHelper;

    List<Info>     mData = new ArrayList<Info>(); ;

   List<String > teachers ;

    private ArrayAdapter adapter = null;
    //TODO
    public static void startAct(Context context) {
        Intent intent = new Intent(context, AddActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);

        mHelper = new DbOpenHelper(this);
        mData =new ArrayList<>();
        teachers =new ArrayList<>();
        getStudentInfoList();
        /**
         * 给下拉栏赋值
         */
        for (int i = 0; i < mData.size(); i++) {
            teachers.add( mData.get(i).getSubject());
        }
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, teachers);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnSubjet.setAdapter(adapter);

    }

    @OnClick({R.id.backList, R.id.submitBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backList:
//                Toast.makeText(AddActivity.this,
//                        "信息还未保存，确认返回吗？",
//                        Toast.LENGTH_SHORT
//                ).show();
                Intent intent = new Intent();
                intent.setClass(AddActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;

            case R.id.submitBtn:
//添加成绩
                final String name = addText1.getText().toString();
                final String className = addText2.getText().toString();
                final String studentNo = addText3.getText().toString();
                final String subjcet = spnSubjet.getSelectedItem().toString();
                final String score = addText4.getText().toString();
                SQLiteDatabase db = mHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("className", className);
                values.put("studentNo", studentNo);
                values.put("subject", subjcet);
                values.put("score", score);
                db.insert("Student", null, values);
                values.clear();
                db.close();
              Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 获得数据库中的科目与教师数据
     */
    private List<Info> getStudentInfoList() {

        SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("Teacher", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String teacher = cursor.getString(cursor.getColumnIndex("teacher"));
                String subject = cursor.getString(cursor.getColumnIndex("subject"));


                mData.add(new Info(teacher, subject));
            } while (cursor.moveToNext());
        }
        cursor.close();

        return mData;
    }
}
